package com.kolon.business.mAccessMan.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.DateUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mAccessMan.controller.MAccessManApplyWriteController")
public class MAccessManApplyWriteController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mAccessMan/mAccessManApplyWrite.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String name = (String)userMap.get("UserName");
		String pw = (String)userMap.get("userPw");
		String companyCD = (String)userMap.get("CompanyCD");
		String deptCode = (String)userMap.get("DeptCode");	
		String Mode = ComUtil.nullChk(request.getParameter("Mode"));
		//
		String companyName = (String)userMap.get("companyname");
		String empNum = (String)userMap.get("EmpNum");
		String deptName = (String)userMap.get("DeptName");
		String titleCode = (String)userMap.get("TitleCode");
		String titleName = (String)userMap.get("TitleName");
		String officeTelNum = (String)userMap.get("OfficeTelNum");
		String mobileNum = (String)userMap.get("MobileNum");
		   
		ModelAndView modelAndView = new ModelAndView();
		// 요청 등록 처리
		if(Mode.equals("Save")) {
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append(url);
			buffer2.append("/access_man.aspx?command=").append("insert");
			buffer2.append("&BUSINESS_CODE=").append(ComUtil.nullChk(request.getParameter("sel_business_code")));
			buffer2.append("&ReqUserID=").append(id);
			buffer2.append("&ReqUserName=").append(name);
			buffer2.append("&ACCESS_AUTH_CODE=").append(ComUtil.nullChk(request.getParameter("sel_access_auth_code")));
			buffer2.append("&Customer=").append(ComUtil.nullChk(request.getParameter("txt_visitor_name")));
			buffer2.append("&CusComName=").append(ComUtil.nullChk(request.getParameter("txt_visitor_comp_name")));
			buffer2.append("&CusDeptName=").append("");
			buffer2.append("&CusTel=").append(ComUtil.nullChk(request.getParameter("txt_visitor_tel_no")));
			buffer2.append("&REASON=").append(ComUtil.getEncode(request.getParameter("txt_visitor_reason"),"EUC-KR"));
			buffer2.append("&VISITOR_PER_CNT=").append(ComUtil.nullChk(request.getParameter("visitor_per_cnt")));
			buffer2.append("&RsvDate=").append(ComUtil.nullChk(request.getParameter("txt_reservation_date")));
			buffer2.append("&START_TIME=").append(ComUtil.nullChk(request.getParameter("txt_reservation_start_hour"))+":"+ComUtil.nullChk(request.getParameter("txt_reservation_start_minute")));
			buffer2.append("&END_TIME=").append(ComUtil.nullChk(request.getParameter("txt_reservation_end_hour"))+":"+ComUtil.nullChk(request.getParameter("txt_reservation_end_minute")));
			buffer2.append("&CompanionDetail=").append(request.getParameter("CompanionDetail"));
			//
			buffer2.append("&ReqComCode=").append(companyCD);
			buffer2.append("&ReqComName=").append(companyName);
			buffer2.append("&EMP_NUM=").append(empNum);
			buffer2.append("&ReqDeptCode=").append(deptCode);
			buffer2.append("&ReqDeptName=").append(deptName);
			buffer2.append("&ReqTitleCode=").append(titleCode);
			buffer2.append("&ReqTitleName=").append(titleName);
			buffer2.append("&ReqTel=").append(officeTelNum);
			buffer2.append("&ReqMobile=").append(mobileNum);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer2.toString(), id, pw);
			String returnUrl = "/business/mAccessMan/mAccessManApplyList.do";
			modelAndView.addObject("scriptType", "009");
			modelAndView.addObject("message_url", returnUrl);
			modelAndView.setViewName("/business/common/alert");
		}else{
			// 장소 1 리스트
			StringBuffer buffer2 = new StringBuffer();
			buffer2.append(url);
			buffer2.append("/access_man.aspx?command=").append("place");
			buffer2.append("&companyCD=").append(companyCD);
			buffer2.append("&deptCD=").append(deptCode);
			// XML DOCUMENT 및 인증 처리
			Document doc2 = ComUtil.getDocument(buffer2.toString(), id, pw);
			Element element2 = doc2.getRootElement();
			List xmlList2 = element2.getChildren("dept");
			Iterator itXml2 = xmlList2.iterator();
			List mapList2 = new ArrayList();
			Map map2 = new HashMap();
			// XML 데이터 
			while (itXml2.hasNext()) {
				Element el = (Element)itXml2.next();
				map2 = new HashMap();
				map2.put("BUSINESS_CODE", el.getChild("BUSINESS_CODE").getText());
				map2.put("BUSINESS_NAME", el.getChild("BUSINESS_NAME").getText());
				mapList2.add(map2);
			}		
			modelAndView.addObject("mapList2", mapList2); //장소1
			modelAndView.addObject("nHour", DateUtil.getHourStr());
		}
		// 리턴
		return modelAndView;
	}
}